#pragma once

#include "../commen/util.hpp"
#include "../commen/log.hpp"
#include <unordered_map>
#include <string>
#include <cassert>
#include <vector>

//文件版本
namespace ns_oj_model
{
    using namespace ns_util;
    using namespace ns_log;

    struct Question
    {
        std::string number; //题目编号，唯一
        std::string title;  //题目的标题
        std::string star;   //难度: 简单 中等 困难
        int cpu_limit;      //题目的时间要求(S)
        int mem_limit;      //题目的空间要去(KB)
        std::string desc;   //题目的描述
        std::string header; //题目预设给用户在线编辑器的代码
        std::string tail;   //题目的测试用例，需要和header拼接，形成完整代码
    };

    const std::string questions_list = "./questions/questions.list";
    const std::string questions_path = "./questions/";

    class OjModel
    {
    public:
        OjModel()
        {
            assert(LoadQuestions(questions_list));
        }

        bool LoadQuestions(const std::string &list)
        {
            std::ifstream in(list);
            if (!in.is_open())
            {
                LogMessage(Error, "open questions_list failed, path: %s\n", list.c_str());
                return false;
            }

            std::string line;
            while (std::getline(in, line))
            {
                std::vector<std::string> vec;
                StringUtil::SplitString(line, vec, " ");
                if (vec.size() != 5)
                {
                    LogMessage(Error, "invalid question format, line: %s\n", line.c_str());
                    for (auto &v : vec)
                    {
                        LogMessage(Error, "vec: %s\n", v.c_str());
                    }
                    continue;
                }
                Question q;
                q.number = vec[0];
                q.title = vec[1];
                q.star = vec[2];
                q.cpu_limit = std::stoi(vec[3]);
                q.mem_limit = std::stoi(vec[4]);
                std::string path = questions_path;
                path += q.number;
                path += "/";
                LogMessage(Debug, "正在加载题目: %s\n", q.number.c_str());
                FileUtil::ReadFile(path + "desc.txt", q.desc, true);
                LogMessage(Debug, "加载题目描述成功, desc: %s\n", q.desc.c_str());
                FileUtil::ReadFile(path + "header.hpp", q.header, true);
                LogMessage(Debug, "加载题目预设代码成功, header: %s\n", q.header.c_str());
                FileUtil::ReadFile(path + "tail.cpp", q.tail, true);
                LogMessage(Debug, "加载题目测试用例成功, tail: %s\n", q.tail.c_str());
                _questions[q.number] = q;
            }
            LogMessage(Info, "加载题库成功\n");
            in.close();
            return true;
        }

        bool GetAllQuestions(std::vector<Question> *out)
        {
            if (_questions.size() == 0)
            {
                LogMessage(Warning, "获取题库失败\n");
                return false;
            }
            for (auto &q : _questions)
            {
                out->push_back(q.second);
            }
            return true;
        }
        
        bool GetOneQuestion(const std::string &number, Question *q)
        {
            const auto& iter = _questions.find(number);
            if (iter != _questions.end())
            {
                *q = iter->second;
                LogMessage(Debug, "获取题目成功, number: %s\n", number.c_str());
                return true;
            }
            LogMessage(Error, "获取题目失败, number: %s\n", number.c_str());
            return false;
        }

    private:
        std::unordered_map<std::string, Question> _questions;//题目编号 -> 题目
    };

}